<template>
  <div class="address-bg">
      <div class="address">
          <van-address-edit :address-info="addressInfo" show-set-default show-search-result
          show-delete :show-area="false" :search-result="searchResult" @save="onSave" @delete="onDelete"/>
      </div>
  </div>
</template>

<script>
import{areaList} from '@vant/area-data'
export default {
  props: ['info'],
  components: {
  },
  data() {
    return {
        id:0,
        areaList,
        searchResult:[],
        addressInfo:{}
    };
  },
  computed: {
  },
  watch: {
  },
  created() {
      this.init()
  },
  methods: {
      onSave(data){
          let form={
              id:this.addressInfo.id,
              name:data.name,
              tel:data.tel,
              area:'北京市-北京市-东城区',
              isDefault:data.isDefault,
              detail:data.addressDetail
          }
          this.$http.post('/api/address/update',form).then(
              response=>{
                  console.log('updataAddress',response.data)
                  if(response.code==0){
                      this.$router.push({name:'address'})
                  }
              },error=>{
                  console.log('updataAddress',error.msg)
              }
          )
      },
      onDelete(){
          this.$http.post(`/api/address/delete/${this.addressInfo.id}`).then(
              response=>{
                  console.log('deleteAddress',response.data)
                  this.$toast.success('删除成功')
                  this.$router.go(-1)
              },error=>{
                  console.log('deleteAddress',error.msg)
              }
          )
      },
      init(){
          this.addressInfo={
              id:this.info.id,
              name:this.info.name,
              tel:this.info.tel,
              addressDetail:this.info.address,
              isDefault:this.info.isDefault
          }
      }
  },
};
</script>
<style scoped lang="less">
.address{
    margin-top: 150px;
    .address-bg{
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgb(247,248,250);
    }
}
</style>